<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/user';
import { login} from '../utils/request'
const userStore = useUserStore();

const email = ref('');
const password = ref('');
const router = useRouter();

const btnLogin =async () => {
  // 这里可以添加登录逻辑
  const r = await login({
    username: email.value,
    pwd: password.value,
  });
  // if(email.value=="a@a.com"&&password.value=="111"){
  //     console.log('Logging in with:', email.value, password.value);
  // 登录成功后跳转到首页
  // debugger
  if(r){
  sessionStorage.setItem('isLoggedIn', 'true');
  const newUser = r;
  userStore.setUser(newUser);
  //把用户信息存到pinia的状态里
  
  router.push('/article');
  }else{
    alert("账号或密码错误")

  }

};
</script>

<template>
  <div class="auth-container">
    <h2>登录</h2>
    <div class="input-container">
      <input v-model="email" type="email" placeholder="邮箱" />
    </div>
    <div class="input-container">
      <input v-model="password" type="password" placeholder="密码" />
    </div>
    <div class="input-container">
      <button @click="btnLogin">登录</button>
    </div>
    <div class="auth-link">
      <RouterLink to="/register">没有账号？注册</RouterLink>
    </div>
    <div class="home-link">
      <RouterLink to="/">返回首页</RouterLink>
    </div>
  </div>
</template>

<style scoped>
.auth-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 2rem;
  background-color: #e6f7ff; /* 恢复浅蓝色背景 */
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.input-container {
  margin-bottom: 1rem;
}

.auth-link {
  display: block;
  margin-top: 1rem;
  color: #1890ff; /* 恢复蓝色链接 */
  text-decoration: none;
}

.auth-link:hover {
  text-decoration: underline;
}

.input-container button {
  padding: 0.75rem 1.5rem;
  border: none;
  background-color: #1890ff; /* 恢复蓝色按钮 */
  color: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
}

.input-container button:hover {
  background-color: #107ad9; /* 悬停时深蓝色 */
}

.home-link {
  margin-top: 1rem;
  text-align: center;
}

.home-link a {
  color: #1890ff; /* 蓝色链接 */
  text-decoration: none;
  font-weight: bold;
  font-size: 1rem;
}

.home-link a:hover {
  text-decoration: underline;
}
</style>